#include "../include/TF_IDF.hpp"
#include <utility>
#include <cmath>

using std::make_pair;

TF_IDF::TF_IDF(int totaldoc)
: _totaldoc(totaldoc)
{

}

unordered_map<string, map<int, long double>> TF_IDF::operator()(const unordered_map<string, map<int, int>> & wordmap,const map<int,int> & wordnum)
{
    unordered_map<string, map<int, long double>> invertmap;
    for(auto it = wordmap.begin(); it != wordmap.end(); ++it)
    {
        //map<int, double> submap;
        for(auto subit = (it->second).begin(); subit != (it->second).end(); ++subit)
        {   
            int num = wordnum.at(subit->first);
            long double TF = (long double)(subit->second) / (long double)num;
            int DF = (it->second).size();
            long double IDF = std::log2((long double)_totaldoc / (long double)(DF + 1));
            long double w = TF * IDF;

            invertmap[it->first].insert(make_pair(subit->first,w));
        }
    }
    return invertmap;
}
